<?php

namespace App\Http\Controllers;

use App\Http\Resources\NotionDbCollection;
use App\Http\Resources\NotionDbResource;
use App\NotionDb;
use App\NotionToken;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class NotionController extends Controller
{
    /**
     * 提交页面到notion
     *
     * @author zkimleung <shoujing.liang@bloomchic.com>
     * @dateTime 2025/01/10
     * @param  Request $request
     * @return void
     */
    public function addPageDB(Request $request)
    {
        // 处理请求
        $json = $request->getContent();
        $data = json_decode($json, true);
        try {
            $uuid =  $request->attributes->get("unionid") ?? $request->attributes->get("openid");
            $token = config('notion.token');
            $data['parent']['database_id'] = config('notion.database_id');

            $client = new Client();
            $res = $client->post('https://api.notion.com/v1/pages', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/json',
                    'Notion-Version' => '2022-06-28',
                ],
                'json' => $data,
            ]);
        } catch (\Throwable $th) {
            return response($th->getMessage(), 401);
        }
        return response($res->getBody(), 200);
    }

}
